/*
 * @Author: lei
 * @Date: 2021-08-02 10:56:04
 * @LastEditTime: 2021-08-02 12:22:32
 * @LastEditors: Please set LastEditors
 * @Description: lambda函数使用，基本形式、值捕获、引用捕获、隐式捕获、返回类型
 * @FilePath: /code/lambda.cpp
 */

#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
void test1()
{
    vector<int> arr = {3, 4, 76, 12, 54, 90, 34};
    sort(arr.begin(), arr.end(), [](int a, int b) ->bool{ return a > b; }); // 降序排序
    for (auto a : arr)
    {
        cout << a << " ";
    }
    cout<<endl;
}
void test2()
{
    int a=10;
    int c=20;
    auto f=[&,a](int b) mutable->void{cout<<"a= "<<a<<" ,b= "<<b<<endl;a=100;};//隐式捕获为引用捕获，显式捕获a
    //要想改变显式捕获的值，需要加mutable关键字，但test2里原a的值不会改变；
    //auto f=[=,&a](int b)->void{cout<<"a= "<<a<<" ,b= "<<b<<endl;a=100;};//隐式捕获为值捕获，隐式捕获a
    function<void(int)> f2=f;
    f(4);
    f2(3);
}
int main()
{
    test1();
    test2();
    return 0;
}